package it.traveldreamWeb.customer;

import it.traveldream.beans.PackageManagerInterface;
import it.traveldream.dto.LocationDTO;
import it.traveldream.dto.TPDTO;
import it.traveldream.exceptions.LocationNotFoundException;
import it.traveldreamWeb.shared.ApplicationDataBean;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

@ManagedBean(name="packageListBean")
@ViewScoped
public class PackageListBean {

	private List<LocationDTO> locations;
	private LocationDTO selectedLocation;
	private TPDTO selectedTP;
	private List<TPDTO> availableTPs = new ArrayList<>();

	@EJB
	private PackageManagerInterface packageManager;

	private String error;
	
	@ManagedProperty(value="#{reservationBean}")
	private ReservationBean session;
	
	@ManagedProperty(value="#{applicationDataBean}")
	private ApplicationDataBean appData;

	@PostConstruct
	private void init(){
		// clean session
		this.session.cleanSession();
		this.session.cleanMessages();
		
		this.locations = this.appData.getLocations();
	}
	
	public void setSession(ReservationBean messageBean) {
		this.session = messageBean;
	}
	
	public ReservationBean getSession(){
		return this.session;
	}
	
	public ApplicationDataBean getAppData() {
		return this.appData;
	}

	public void setAppData(ApplicationDataBean appData) {
		this.appData = appData;
	}

	public String getError() {
		return this.error;
	}

	public void setError(String error) {
		this.error = error;
	}
	
	public List<TPDTO> getAvailableTPs() {
		return this.availableTPs;
	}

	public void setAvailableTPs(List<TPDTO> availableTPs) {
		this.availableTPs = availableTPs;
	}
	
	public LocationDTO getSelectedLocation() {
		return this.selectedLocation;
	}

	public void setSelectedLocation(LocationDTO location) {
		this.selectedLocation = location;
	}
	
	public TPDTO getSelectedTP() {
		return this.selectedTP;
	}

	public void setSelectedTP(TPDTO selectedTP) {
		this.selectedTP = selectedTP;
	}

	public List<LocationDTO> getLocations(String startingWith){
		List<LocationDTO> suggestions = new ArrayList<>();
		
		for (LocationDTO l : this.locations){
			if (l.getName().toLowerCase().contains(startingWith) ||
					l.getName().toUpperCase().contains(startingWith))
				suggestions.add(l);
		}
		return suggestions;
	}

	public void locationConfirmed(){
		if (this.selectedLocation == null)
			this.setError("Please, select a location first");
		else{
			this.setError("");
			try {
				this.availableTPs = this.packageManager.getTPsByLocation(this.selectedLocation);
			} catch (LocationNotFoundException e) {
				this.setError("Location unknown!");
			} catch (Exception e) {
				this.setError("Internal error! Pleare try again");
			}
		}
	}
	
	public String viewTP(){
		this.setError("");
		try {
			if (this.selectedTP == null){
				this.setError("Please, select a travel package first");
				return "";
			}
			
			String params = "tpid=" + this.selectedTP.getTPId();
			return "PackageView?faces-redirect=true&" + params;
		} catch (Exception e) {
			this.setError("Internal error! Pleare try again");
			return "";
		}
	}
}
